/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.atom.saop;

import android.app.AlertDialog;
import android.app.Application;
import android.text.TextUtils;

import com.atom.module.logger.Logcat;
import com.saop.core.SAOP;
import com.saop.core.api.DialogHandler;
import com.saop.core.utils.PermissionUtils;
import com.saop.core.utils.StringUtils;

import java.util.List;

import timber.log.Timber;

/**
 * <pre>
 *     desc   :
 *     author : xuexiang
 *     time   : 2018/4/22 下午7:13
 * </pre>
 */
public class App extends Application {

    public static final String TRY_CATCH_KEY = "getNumber";

    public static final int INTERCEPT_LOGIN = 10;

    @Override
    public void onCreate() {
        super.onCreate();
        SAOP.init(this);
        SAOP.setOnPermissionDeniedListener(new PermissionUtils.OnPermissionDeniedListener() {
            @Override
            public void onDenied(List<String> permissionsDenied) {
                String s = StringUtils.listToString(permissionsDenied);
                Timber.e("权限就是没有 自己跳转去申请吧" + s);
            }
        });

        SAOP.setExceptionHandler((flag, throwable) -> throwable.getLocalizedMessage() + " --- > 发生异常啦, 我看到了 , 给你一个正常的你去完把");

        SAOP.setInterceptHandler((type, joinPoint) -> {
            switch (type) {
                case 1: {
                    Timber.e("拦截器[1] , 我负责监督打印 ");
                    break;
                }
                case 2: {
                    Timber.e("拦截器[2] , 我负责拦截");
                    return true;
                }
            }
            return false;
        });

        SAOP.setDialogHandler((context, parameter, result) -> {
            final AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setTitle(parameter.getTitle())
                    .setMessage(parameter.getMessage());
            String positive = parameter.getPositive();
            if (!TextUtils.isEmpty(positive)) {
                builder.setPositiveButton(positive, (dialog, which) -> {
                    result[0] = DialogHandler.Statue.POSITIVE;
                    dialog.dismiss();
                });
            }

            String negative = parameter.getNegative();
            if (!TextUtils.isEmpty(negative)) {
                builder.setNegativeButton(negative, (dialog, which) -> {
                    result[0] = DialogHandler.Statue.NEGATIVE;
                    dialog.dismiss();
                });
            }

            String neutral = parameter.getNeutral();
            if (!TextUtils.isEmpty(neutral)) {
                builder.setNeutralButton(neutral, (dialog, which) -> {
                    result[0] = DialogHandler.Statue.NEUTRAL;
                    dialog.dismiss();
                });
            }
            final AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(parameter.isTouchOutSideCancel());
            return alertDialog;
        });
        Logcat.init(BuildConfig.DEBUG , null);
    }
}
